/**
 * Created by Mark_ren on 2016/10/10.
 * 苟富贵、無相忘
 */
var express = require('express');
var router = express.Router();
var moment = require("moment");
var co = require('co');
var seqModule = require('./createModules');
var seq = new seqModule();


var formidable = require('formidable');
var getIP = require('./getIP');
var fs = require('fs');

//分页查询点播分类内容
router.get('/getMetSortPage', function(req, res, next) {
    co(function () {
        var page=parseInt(req.query.page);
        var params={};
        var paramT={};
        var paramU={};
        if(req.query.id != undefined){
            params["id"]=req.query.id;
        }
        //模糊查询
        if(req.query.sortTitle != undefined){
            paramU["$like"]="%"+req.query.sortTitle+"%";
            params["sortTitle"]=paramU;

        }
        paramT["where"]=params;
        paramT["order"]=[["createTime",'DESC']];
        if(req.query.page != undefined && req.query.rows !=undefined){
            paramT["offset"]=parseInt(req.query.page);
            paramT["limit"]=parseInt(req.query.rows);
        }
        seq.metSortMenuModule.findAndCountAll(paramT).then(function(result){
            res.json({"success":true,"page":page,"total":result.count,"result":result.rows});  });
    }).catch(function(e) {
        res.json({'success':false,'msg':'查询失败'});
        console.log(e);
    })
});

//添加点播分类
router.post('/addMetSort', function(req, res, next) {
    co(function () {
        if (req.method.toLowerCase() == 'post') {
            var form = new formidable.IncomingForm();
            form.type = true;
            form.on("progress", function (bytesReceived, bytesExpected) {
                console.log("progress!" + bytesReceived + " B____" + (bytesExpected / 1024 / 1024).toFixed(2) + " MB");
                // 百分比
                var percent = Math.round(bytesReceived / bytesExpected * 100);
                console.log("percent:" + percent + "%");
                // 存入
                //sessions.setSession(req,res,opts);
            });
            form.parse(req, function (err, fields, files) {
                seq.metSortMenuModule.create(fields).then(function(results){
                    res.redirect(getIP+'metVideo/metSortList.html');
                });
            });
        }

    }).catch(function(e) {
        res.json({'success':false,'msg':'添加失败'});
        console.log(e);
    })
});

//删除
router.get('/deleteMetSort', function(req, res, next) {
    co(function () {
        var params=req.query.params;
        var arr=params.split(",");
        var paramT=[arr];
        seq.metSortMenuModule.destroy({'where':{'id':paramT}}).then(function(results){
            res.json({"success":true,"msg":"删除成功","rows":results});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'删除失败'});
        console.log(e);
    })
});

//查询点播分类列表
router.get('/getMetSorts', function(req, res, next) {
    co(function () {
        var params={};
        var paramT={};
        var paramU={};
        if(req.query.id != undefined){
            params["id"]=req.query.id;
        }
        //模糊查询
        if(req.query.sortTitle != undefined){
            paramU["$like"]="%"+req.query.sortTitle+"%";
            params["sortTitle"]=paramU;

        }
        paramT["where"]=params;
        seq.metSortMenuModule.findAll(paramT).then(function(result){
            res.json({"success":true,"msg":"查询成功","result":result});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'查询失败'});
        console.log(e);
    })
});
//修改点播分类内容
router.post('/updateMetSort', function(req, res, next) {
    co(function () {
        if (req.method.toLowerCase() == 'post') {
            var form = new formidable.IncomingForm();
            form.type = true;
            form.on("progress", function (bytesReceived, bytesExpected) {
                console.log("progress!" + bytesReceived + " B____" + (bytesExpected / 1024 / 1024).toFixed(2) + " MB");
                // 百分比
                var percent = Math.round(bytesReceived / bytesExpected * 100);
                console.log("percent:" + percent + "%");
                // 存入
                //sessions.setSession(req,res,opts);
            });
            form.parse(req, function (err, fields, files) {
                var paramT = {};
                paramT.where = {"id":fields.id};
                delete fields.id;
                seq.metSortMenuModule.update(fields,paramT
                ).then(function(results){
                    res.redirect(getIP+'metVideo/metSortList.html');
                });
            });
        }

    }).catch(function(e) {
        res.json({'success':false,'msg':'修改失败'});
        console.log(e);
    })
});



module.exports = router;